package com.propertypoint.persitence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.propertypoint.exception.AuthenticationException;
import com.propertypoint.presentation.model.Agent;
import com.propertypoint.presentation.model.BaseUser;
import com.propertypoint.util.DBUtil;
import com.propertypoint.util.PropertyPointConstants;

public class AuthenticationDAOImpl implements AuthenticationDAO{

	private static final String SELECT_USER="select * from agent where username =?";
	
	
	 
	@Override
	public BaseUser authenticateUser(String username, String password) throws Exception {
		Connection con = DBUtil.getDBUtil().getConnection();
		PreparedStatement preparedStatement = con.prepareStatement(SELECT_USER);
		preparedStatement.setString(1, username);
		ResultSet rs = preparedStatement.executeQuery();
		int count=0;
		while (rs.next()) {
			count++;
			String pw=rs.getString("password");
			if(pw!=null&&pw.equals(password)){
				//FIRST_NAME,LAST_NAME,PHONE_NUMBER,EMAIL,ADDRESS,USERNAME,PASSWORD,DOB,ZIP_CODE,GENDER,LICENSE_NO,COMPANY_NAME
				String licenceNo=rs.getString("LICENSE_NO");
				String companyName=rs.getString("COMPANY_NAME");
				
				BaseUser user=new Agent(licenceNo, companyName);
				user.setEmail(rs.getString("EMAIL"));
				user.setUsername(username);
				user.setFirstName(rs.getString("FIRST_NAME"));
				user.setLastName(rs.getString("LAST_NAME"));
				user.setPhoneNumber(rs.getString("PHONE_NUMBER"));
				user.setAddress(rs.getString("ADDRESS"));
				user.setDateOfBirth(rs.getString("DOB"));
				user.setZipCode(rs.getString("ZIP_CODE"));
				user.setGender(rs.getString("GENDER"));
				if (rs.getString("profile_picture") != null) {
					user.setPictureUrl("img/profilepics/"
							+ rs.getString("profile_picture"));
					user.setPicture(rs.getString("profile_picture"));
				}
				user.setId(rs.getLong("ID"));
				user.setUsername(username);
				return user;
			}else{
				throw new AuthenticationException(PropertyPointConstants.LOGIN_INVALID_PASSWORD);
			}
		}
		if(count==0){
			throw new AuthenticationException(PropertyPointConstants.LOGIN_USER_NOT_FOUND_CODE);
		}
		return null;
	}
	
	@Override
	public BaseUser getUser(String username) throws Exception {
		Connection con = DBUtil.getDBUtil().getConnection();
		PreparedStatement preparedStatement = con.prepareStatement(SELECT_USER);
		preparedStatement.setString(1, username);
		ResultSet rs = preparedStatement.executeQuery();
		while (rs.next()) {
				//FIRST_NAME,LAST_NAME,PHONE_NUMBER,EMAIL,ADDRESS,USERNAME,PASSWORD,DOB,ZIP_CODE,GENDER,LICENSE_NO,COMPANY_NAME
				String licenceNo=rs.getString("LICENSE_NO");
				String companyName=rs.getString("COMPANY_NAME");
				
				BaseUser user=new Agent(licenceNo, companyName);
				user.setEmail(rs.getString("EMAIL"));
				user.setUsername(username);
				user.setFirstName(rs.getString("FIRST_NAME"));
				user.setLastName(rs.getString("LAST_NAME"));
				user.setPhoneNumber(rs.getString("PHONE_NUMBER"));
				user.setAddress(rs.getString("ADDRESS"));
				user.setDateOfBirth(rs.getString("DOB"));
				user.setZipCode(rs.getString("ZIP_CODE"));
				user.setGender(rs.getString("GENDER"));
				if (rs.getString("profile_picture") != null) {
					user.setPictureUrl("img/profilepics/"
							+ rs.getString("profile_picture"));
					user.setPicture(rs.getString("profile_picture"));
				}
				user.setId(rs.getLong("ID"));
				return user;
			 
		}
		 
		return null;
	}

}
